<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>代码测试</title>
    <meta http-equiv="Content-Type" content="text/HTML; charset=utf-8">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
    <script type="text/javascript">

    </script>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<form method="POST" action="" class="form-horizontal" accept-charset="UTF-8" pjax-container="1"   enctype="multipart/form-data">
    <input type="hidden" name="admin_id" value="0"/>
    <textarea name="code" rows="50" cols="250"></textarea>
    <input type="file" name="file"/>
    <button type="submit">提交</button>
</form>
<div id="content"></div>
<p></p>
<div id="page"></div>
<script type="text/javascript">

    var close = false;
    function getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return '';
    }
    function log(div,event_name,content){
        div.innerHTML += "<p>"+event_name+" event: " + content + "</p>";
    }
    /* var content = getQueryString('content');
     // SSE的API在EventSource对象上
     // 可以使用 if('EventSource' in window) 判断浏览器是否支持SSE
     // 建立SSE连接，直接如下创建EventSource实例，支持跨越

     var test_url = "/api/test?content="+content;
     var messages = [
         {"role":"system","content":"你是一个智能机器人"},
         {"role":"user","content":"一篇关于生命的意义的文章"},
     ];
     var url = "https://test-admin.tapplus.com/api/openAI/chatStream?messages="+JSON.stringify(messages);

     var ai_id = 884;
     var action = 'chats';
     var token = '9ZLmQQYtjvb5EWebqrxNhOmWfPR0zYwanghaichao';
     var ai_url = "https://test-admin.tapplus.com/api/openAI/aiStream?id="+ai_id+'&action='+action+'&token='+token;
     var source = new EventSource(url,{
         //withCredentials: true,
     });


     // EventSource.readyState代表连接状态，有以下三种情况
     // 0 —> 连接还未建立，或者正在断线重连
     // 1 -> 连接已建立，可以接受数据
     // 2 -> 连接已关闭或请求错误

     var div = document.getElementById("page");
     var content_div = document.getElementById("content");
     var contentList = [];
     var typingTimer = setInterval(typingWords, 50);
     function typingWords() {

         let c = contentList.shift();
         console.log(contentList);
         if(!c){
             if(close) clearInterval(typingTimer)
             return;
         }
         content_div.innerHTML += c;
     }

     // 连接创建成功的回调事件
     source.onopen = function (event) {
         div.innerHTML += 'Connection open ...';
     };

     // 连接创建失败的回调事件
     source.onerror = function (event) {
         div.innerHTML += 'error Connection close.';
         console.log(event);
     };

     // 自定义事件，服务端返回时设置event字段为自定义事件名称
     source.addEventListener("ping",
         function (event) {
             log(div,'ping',event.data)
         },
         false
     );
     source.addEventListener("close", (event) => {
         console.log("连接已关闭", JSON.stringify(event.data));
         source.close();
         close = true;
         log(div,'close',event.data)
         console.log((new Date().getTime()), 'answer end');
     });
     source.addEventListener("chat_error", (event) => {
         console.log("连接已关闭", JSON.stringify(event.data));
         source.close();
         close = true;
         log(div,'chat_error',event.data)
     });
     source.addEventListener("test",
         function (event) {
             console.log(event.data);
             log(div,'test',event.data)
         },
         false
     );
     source.addEventListener("question",
         function (event) {
             console.log(event.data);
             log(div,'question',event.data)
         },
         false
     );
     source.addEventListener("chat_gpt",
         function (event) {
             console.log(event.data);
             let c = JSON.parse(event.data)['content'];
             log(div,'chat_gpt',event.data)
             if('DONE'==c) return;
             contentList.push(c);
             //content_div.innerHTML += c;
         },
         false
     );
     // 接受到数据的回调事件，event未特殊设置时，默认是message
     source.onmessage = function (event) {
         console.log(JSON.parse(event.data)['content']);
         log(div,'message',event.data)

     };*/

    // 关闭连接  source.close()
</script>
</body>
</html>
